// pages/search/search.js
import ajax from "../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 标题栏高度
     */
    historyViewHeight: 60,
    /**
     * 显示/隐藏历史搜索
     */
    historyShow: true,
    /**
     * 历史搜索 数据
     */
    historySearchList: [],
    /**
     * 园区列表
     */
    housingList: [],
    recommendList: [], // 推荐园区列表
    recommendShow: false,
    formData: {
      pageSize: 10,
      pageNum: 1,
      search: ''// 搜索关键字
    },
    historyHeight: 46,
    city: ''
  },
  /**
   * 搜索框变化事件
   */
  inputChange(e) {
    this.data.formData.search = e.detail.value.replace(/\s*/g, "");
  },
  /**
   * 搜索框获取焦点事件
   */
  inputFocus(e) {
    this.setData({ historyShow: true })
  },
  /**
   * 重置事件
   */
  resetEvent() {
    if (!this.data.formData.search) return ajax.showToast('请输入内容后在重置')
    this.setData({ historyShow: false, 'formData.search': '' })
    this.data.formData.pageNum = 1
    this.getGardenkList()
  },

  /**
   * 搜索框确认事件
   */
  inputConfirm(event) {
    const value = event?.currentTarget?.dataset?.value || ''
    let { formData, historySearchList } = this.data
    formData.search = value || formData.search || ''
    const boo = historySearchList.some(item => item == formData.search)
    if (!boo && formData.search) {
      historySearchList.push(formData.search)
      wx.setStorageSync('historySearchList', historySearchList)
    }
    this.setData({ historyShow: false, historySearchList, formData })
    this.getGardenkList()
    this.getElement()
  },
  /**
   * 清空历史搜索记录
   */
  clearEvent() {
    wx.removeStorageSync('historySearchList')
    this.setData({ historySearchList: [] })
    this.getElement()
  },

  /**
   * 获取园区列表
   */
  async getGardenkList() {
    const { formData, housingList } = this.data
    ajax.showLoading()
    const res = await ajax.get('/enterpriseservice/home/gardenkHousingList', formData).finally(() => wx.hideLoading(), wx.stopPullDownRefresh());
    const list = formData.pageNum == 1 ? res.rows : housingList.concat(res.rows)
    this.setData({ housingList: list })
  },

  /**
    * 获取园区推荐
    */
  async getRecommendParkList() {
    const res = await ajax.get(`/enterpriseservice/home/recommend?city=${this.data.formData.city}`)
    const list = res.rows
    this.setData({ recommendList: list })
  },
  /**
    * 查看授权
    */
  async getSetting() {
    const setting = await ajax.getSetting()
    this.setData({ recommendShow: setting['scope.userLocation'] })
  },
  async getElement() {
    const res = await ajax.getElement('#history',this)
    this.setData({
      historyHeight: res.height
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const historySearchList = wx.getStorageSync('historySearchList') || []

    this.setData({
      'formData.city': options.city || '',
      historySearchList,
      historyViewHeight: ajax.getHeadlineHeight()
    })
    this.getElement()
    this.getRecommendParkList()
    this.getSetting()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.formData.pageNum = 1;
    this.getGardenkList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.formData.pageNum += 1;
    this.getGardenkList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})